package com.ytjj.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.quartz.config.SupplyApiConfig;
import com.ytjj.quartz.model.ProductStockResponse;
import com.ytjj.quartz.service.ProductInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ProductTask {
    @Autowired
    private ProductInfoService productInfoService;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Scheduled(cron = "0 3/40 * * * ?")
    public void updateProductStock() {
        log.info("===同步更新商品库存开始===");
        List<Integer> bankerProductIds = productInfoService.queryProductByStatus(1);
        if (!CollectionUtils.isEmpty(bankerProductIds)) {
            int forNum = 1;
            if (bankerProductIds.size() > 50) {
                forNum = bankerProductIds.size() / 50;
                if ((bankerProductIds.size() % 50) > 0) {
                    forNum = forNum + 1;
                }
            }
            for (int i = 0; i < forNum; i++) {
                List<Integer> newList = null;
                if (i == forNum - 1) {
                    newList = bankerProductIds.subList(i * 50, bankerProductIds.size());
                }else {
                    newList = bankerProductIds.subList(i * 50, i * 50 + 50);
                }
                Map<String, String> param = new HashMap<>();
                param.put("bankerProductId", Joiner.on(",").join(newList));
                String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/product/getProductStock", param, supplyApiConfig.getHeadsMap());
                CommonResult commonResult = JSONObject.parseObject(resultJson, CommonResult.class);
                if (200 != commonResult.getCode() || StringUtils.isEmpty(String.valueOf(commonResult.getData()))) {
                    continue;
                }
                List<ProductStockResponse> bankerProductIdList = JSONArray.parseArray(commonResult.getData().toString(), ProductStockResponse.class);
                bankerProductIdList.stream().forEach(item -> {
                    if (null != item.getBankerProductId() && null != item.getStock()) {
                        productInfoService.updateProductStock(item.getBankerProductId(), item.getStock());
                    }
                });
            }
        }

        log.info("===同步更新商品库存完成===");
    }

    /**
     * 定时修改商品评论时间
     */
    @Scheduled(cron = "0 2 * * * ?")
    public void updateProductReviews(){
        log.info("===定时修改商品评论时间===");
        productInfoService.updateProductReviews();
    }
}
